///////////////////////////////////////////////////////////////////////////////
//
//
//
///////////////////////////////////////////////////////////////////////////////

package
{
    import controller.AnalyticGetCommand;
    import controller.CollectUserStatisticCommand;
    import controller.ConnectionEstablishedCommand;
    import controller.CurrencyRatesGetCommand;
    import controller.GetAuthTokenCommand;
    import controller.GetCountyInfoCommand;
    import controller.LogGetCommand;
    import controller.PrepareModelCommand;
    import controller.PrepareViewCommand;
    import controller.RecordAddCommand;
    import controller.RecordDeleteCommand;
    import controller.RecordUpdateCommand;
    import controller.StartUpCommand;
    import controller.UserAuthorizationCommand;
    import controller.UserPasswordCommand;
    import controller.UserSignOutCommand;

    import controller.UserSigninCommand;

    import controller.UserSignupCommand;

    import model.ElfinProxy;

    import mx.core.FlexGlobals;

    import org.puremvc.as3.patterns.facade.Facade;

import view.AppPreloader;

    public class AppFacade extends Facade
    {
        /**
         *  First notification sended by preloader for initialize application.
         */
        public static const STARTUP:String = "startup";

        /**
         *
         */
        public static const PREPARE_MODEL:String = "prepareModel";

        /**
         *
         */
        public static const PREPARE_VIEW:String = "prepareView";

        /**
         *  Create
         */
        public static const USER_AUTHORIZATION:String = "retriveUserAuthorization";

        /**
         *  Notification for display dialog to user. This notification handled by any
         *  mediator. It not mapped to command.
         */
        public static const SHOW_DIALOG:String = "showDialog";

        public static const COLLECT_USER_STATISTIC:String = "collectUserStatistic";

        public static const ANALYTIC_GET:String = "analyticGet";

        public static function getInstance():AppFacade
        {
            if (instance == null)
                instance = new AppFacade();
            return instance as AppFacade;
        }


        //-------------------------------------------------------------------------
        //
        //  Public API for Preloader class. This methods invoked by AppPreloader.
        //
        //-------------------------------------------------------------------------

        /**
         * startup command invoket by preloader. Preloader passed to parameter.
         * @param AppPreloader
         */
        public function startup(preloader:AppPreloader):void
        {
            this.preloader = preloader;

            sendNotification(STARTUP, preloader);

            var sources:String = getParam("base_url");
            sendNotification(Config.TOKEN_GET, sources.split(","));

        }

        /**
         *  Loading call back function has a signature
         *  <code>loadingCallback(loadingStatusCode:uint):void</code>
         *
         *  Injected by preloader.
         */
        public function completeCallback(status:uint):void
        {
            preloader.completeCallback(status);
        }

        public function setPreloaderString(message:String):void
        {
            preloader.setPreloaderString(message);
        }



        //-------------------------------------------------------------------------
        //
        // Facade override methods
        //
        //-------------------------------------------------------------------------

        override protected function initializeController():void
        {
            super.initializeController();
            registerCommand(STARTUP, StartUpCommand);
            registerCommand(PREPARE_MODEL, PrepareModelCommand);
            registerCommand(PREPARE_VIEW, PrepareViewCommand);

            registerCommand(ANALYTIC_GET, AnalyticGetCommand);
            registerCommand(COLLECT_USER_STATISTIC, CollectUserStatisticCommand);

            // Work with external currency rates service
            registerCommand(Config.CURRENCIES_GET, CurrencyRatesGetCommand);

            // Work with authncity service
            registerCommand(Config.TOKEN_GET, GetAuthTokenCommand);
            registerCommand(ElfinProxy.CONNECTION_ESTABLISHED, ConnectionEstablishedCommand);
            registerCommand(USER_AUTHORIZATION, UserAuthorizationCommand);

            // Work with
            registerCommand(Config.COUNTY_INFO_GET, GetCountyInfoCommand);

            // Work with user
            registerCommand(Config.USER_SIGN_IN, UserSigninCommand);
            registerCommand(Config.USER_SIGN_UP, UserSignupCommand);
            registerCommand(Config.USER_SIGN_OUT, UserSignOutCommand);
            registerCommand(Config.USER_PASSWORD, UserPasswordCommand);

            // Work with records
            registerCommand(Config.LOG_GET, LogGetCommand);
            registerCommand(Config.RECORD_ADD, RecordAddCommand);
            registerCommand(Config.RECORD_DELETE, RecordDeleteCommand);
            registerCommand(Config.RECORD_UPDATE, RecordUpdateCommand);
        }




        //-------------------------------------------------------------------------
        //
        //  Private
        //
        //-------------------------------------------------------------------------


        private var preloader:AppPreloader = null;

        /**
         * FIXME: Copypase from PrepareModelCommand
         * @param paramName
         * @return
         */
        private function getParam(paramName:String):String
        {
            var param:Object = FlexGlobals.topLevelApplication.parameters;
            if (paramName in param && param[paramName])
                return param[paramName];
            return "";
        }

    }
}
